package com.sphd.beautysalon.model;


import com.android.volley.VolleyError;
import com.sphd.beautysalon.info.VipInfo;
import com.sphd.beautysalon.interfaces.VolleyJsonObjectCallBack;
import com.sphd.beautysalon.presenter.VipPresenterImpl;
import com.wzh.frame.utils.JsonUtil;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.List;

/**
 * Created by Hu on 2016/11/09
 */

public class VipModelImpl implements VipModel, VolleyJsonObjectCallBack {
    VipPresenterImpl vipPresenter;

    @Override
    public void sendUrl(String url, VipPresenterImpl vipPresenter) {
        volleyUtil.setJsonObjectRequest(url, this);
        this.vipPresenter = vipPresenter;
    }

    @Override
    public void delUrl(String url, VipPresenterImpl vipPresenter) {
        this.vipPresenter = vipPresenter;
        volleyUtil.setJsonObjectRequest(url, new VolleyJsonObjectCallBack() {
            @Override
            public void onResponse(JSONObject response) {
                try {
                    int res=response.getInt("res");
                    vipPresenter.recRes(res);
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onErrorResponse(VolleyError error) {
                vipPresenter.sendError(error);
            }
        });
    }

    @Override
    public void onResponse(JSONObject response) {
        try {
            String listString;
            int com = 0,dis = 0,topup=0,topupno=0;
            if (response.has("grade0sum")){

             com = response.getInt("grade0sum");
            }
            if (response.has("grade1sum")){

             dis = response.getInt("grade1sum");
            }
            if (response.has("grade2sum")){

             topup = response.getInt("grade2sum");
            }

            if(response.has("grade3sum")){
                topupno=response.getInt("grade3sum");
            }

            vipPresenter.sendRes(com,dis,topup,topupno);
            if (response.has("list")){
                listString=response.getString("list");
            }else {
                listString=response.getString("res");
            }
            List<VipInfo> vipInfos= JsonUtil.getListFromString(listString, VipInfo.class);
            vipPresenter.sendList(vipInfos);
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void onErrorResponse(VolleyError error) {
        vipPresenter.sendError(error);
    }
}